package com.glory.shsi1rpt

import com.ivan.utils.PathUtil

/**
 * @Program: shsi1rpt
 * @Description: SPWR COA-客户一
 * @Author: Ivan
 */
class SpwrCoaCustFirstController {

    def dynamicQueryService

    def spwrCoaCustFirst() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def reportList = []
        def tableHeadList = []
        def totalCount = 0
        def sysQueryName
        def sysQueryPath = PathUtil.getPath(this, request)
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'spwrCoaCustFirstList'
            reportList = dynamicQueryService.queryForList(params)
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            sysQueryName = params.SYS_QUERY_NAME
            totalCount = reportList.size()
        }
        [
                pageCurrent   : pageCurrent,
                pageSizeList  : pageSizeList,
                pageSize      : pageSize,
                startno       : params.startno ?: '',
                endno         : params.endno ?: '',
                startTime     : params.startTime ?: '',
                endTime       : params.endTime ?: '',
                partName      : params.partName ?: '',
                lotId         : params.lotId ?: '',
                startBoxLot   : params.startBoxLot ?: '',
                endBoxLot     : params.endBoxLot ?: '',
                tableHeadList : tableHeadList,
                reportList    : reportList,
                totalCount    : totalCount,
                SYS_QUERY_PATH: sysQueryPath,
                SYS_QUERY_NAME: sysQueryName
        ]
    }

    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.startTime = params.startTime != null ? URLDecoder.decode(params.startTime, 'UTF-8') : params.startTime
        params.endTime = params.endTime != null ? URLDecoder.decode(params.endTime, 'UTF-8') : params.endTime
        params.SYS_QUERY_NAME = 'spwrCoaCustFirstList'
        def reportList = dynamicQueryService.queryForList(params)
        def tableHeadList = []
        if (reportList.size() > 0) {
            tableHeadList = reportList[0].keySet()
        }
        String filename = new String("spwrCoaCustFirstList.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [reportList: reportList, tableHeadList: tableHeadList, startno: params.startno ?: '', endno: params.endno ?: ''])
    }
}